using UnityEngine;

public class ScaleGUI : MonoBehaviour
{
	public float size = 1f;

	public float maxWidth;

	public bool aspectFromTexture;

	private GUITexture tex;

	private int textureHeight;

	private int textureWidth;

	private int screenWidth;

	private float xPosition;

	private float textureAspectRatio;

	private float scaledHeight;

	private float scaledWidth;

	private void Awake()
	{
		tex = GetComponent<GUITexture>();
		screenWidth = Screen.width;
		scaledWidth = (float)screenWidth * size;
		ClampSize();
		xPosition = (float)(screenWidth / 2) - scaledWidth / 2f;
		if (!aspectFromTexture)
		{
			textureHeight = (int)tex.pixelInset.height;
			textureWidth = (int)tex.pixelInset.width;
			textureAspectRatio = (float)textureWidth / (float)textureHeight;
			scaledHeight = scaledWidth / textureAspectRatio;
			tex.pixelInset = new Rect(xPosition, (0f - scaledHeight) * 0.5f, scaledWidth, scaledHeight);
		}
	}

	public void UpdateScale()
	{
		if (aspectFromTexture)
		{
			textureHeight = tex.texture.height;
			textureWidth = tex.texture.width;
			ClampSize();
			textureAspectRatio = (float)textureWidth / (float)textureHeight;
			scaledHeight = scaledWidth / textureAspectRatio;
			tex.pixelInset = new Rect(xPosition, (0f - scaledHeight) * 0.5f, scaledWidth, scaledHeight);
		}
	}

	private void ClampSize()
	{
		if (maxWidth > 0f && scaledWidth > maxWidth)
		{
			scaledWidth = maxWidth;
		}
	}

	private void OnEnable()
	{
		UpdateScale();
	}
}
